package com.herench.bs.agent.impl;

import com.herench.bs.agent.SatisfactionInHosAgent;
import com.herench.bs.dao.ISatisfactionInHosDao;
import com.herench.bs.dao.ISatisfactionOutpatientDao;
import com.herench.bs.dao.IUserDao;
import com.herench.bs.support.WsClientFactory;
import com.herench.bs.utils.DataFormatUtils;
import com.herench.bs.utils.IdGenerator;
import org.apache.cxf.endpoint.Client;
import org.apache.log4j.Logger;
import org.dom4j.Document;
import org.dom4j.DocumentHelper;
import org.dom4j.Element;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.Date;
import java.util.List;
import java.util.Map;

@Service
@Transactional
public class SatisfactionInHosAgentImpl implements SatisfactionInHosAgent {
    @Autowired
    private IUserDao userDao;
    @Autowired
    private ISatisfactionInHosDao satisfactionInHosDao;
    @Autowired
    private ISatisfactionOutpatientDao satisfactionOutpatientDao;

//    @Value("${his_ws_url}")
//    private String hisWsUrl;
    private String hisWsUrl = "http://117.8.229.8:9000/router/webservice/webRouterService?wsdl";
    private static final Logger logger = Logger.getLogger(SatisfactionInHosAgentImpl.class);
    @Override
    public String getOutpatient(List<Map<String, Object>> list) throws Exception{
       //        satisfactionInHosDao.add(el);
        Document tplDoc = getTplDoc("SaveSatisFactionRecord");
        Element dataRowElement = tplDoc.getRootElement().element("CommitData").addElement("Record");
        for (Map<String, Object> subCostMap : list) {
            Map<String,Object> user = userDao.findName((String) subCostMap.get("phone"));
//            String idCard = (String) user.get("idCard");
            String appointId = (String) subCostMap.get("questionId");
            String name = (String) user.get("name");
            String answer = (String) subCostMap.get("answer");
            String cause = (String) subCostMap.get("cause");
            String phone = (String) subCostMap.get("phone");
            String deptName = (String) subCostMap.get("deptName");
            Element dataRowOrder =  dataRowElement.addElement("DataRow");
            dataRowOrder.addAttribute("PROBLEM_ID", appointId);
            if(!"".equals(answer) && answer != null){
                dataRowOrder.addAttribute("OPTION_ANSWER", answer);
            }else {
                dataRowOrder.addAttribute("OPTION_ANSWER", "1");
            }
            dataRowOrder.addAttribute("OPINION", cause);
            dataRowOrder.addAttribute("CHANNEL", "微信").addAttribute("PATIENT_ID", "").addAttribute("PATIENT_NAME", name);
            dataRowOrder.addAttribute("VISIT_NO", "").addAttribute("PHONE_NO", phone).addAttribute("DEPT_CODE", "");
            dataRowOrder.addAttribute("DEPT_NAME", deptName);
        }

        String data = tplDoc.asXML();
        logger.info("data:::::::::::::"+data);
        Client client = WsClientFactory.getInstance().getWsClient(hisWsUrl);
        Object[] results = client.invoke("send",data);
        String originalResXmlMessage = (String) results[0];
        logger.info("originalResXmlMessage:::::::::::::"+originalResXmlMessage);
        Document document = DocumentHelper.parseText(originalResXmlMessage);
        Element resultInfo = document.getRootElement().element("Result").element("Info");
        String executeStatus = resultInfo.attributeValue("EXECUTE_STATUS");
        String executeMessage = resultInfo.attributeValue("EXECUTE_MESSAGE");
        logger.info("executeMessage::::::::::::::::::"+executeMessage);
        return executeStatus;
    }

    @Override
    public String getInHos(List<Map<String, Object>> list) throws Exception{
//        satisfactionOutpatientDao.add(el);

        Document tplDoc = getTplDoc("SaveSatisFactionRecord");
        Element dataRowElement = tplDoc.getRootElement().element("CommitData").addElement("Record");
        for (Map<String, Object> subCostMap : list) {

//            String idCard = (String) user.get("idCard");
            String appointId = (String) subCostMap.get("questionId");
            String name = (String) subCostMap.get("name");
            if("".equals(name) || name == null){
                Map<String,Object> user = userDao.findName((String) subCostMap.get("phone"));
                if(user != null){
                    name = (String) user.get("name");
                }
            }
            String answer = (String) subCostMap.get("answer");
            String cause = (String) subCostMap.get("cause");
            String phone = (String) subCostMap.get("phone");
            String deptName = (String) subCostMap.get("deptName");
            Element dataRowOrder =  dataRowElement.addElement("DataRow");
            dataRowOrder.addAttribute("PROBLEM_ID", appointId);
            if(!"".equals(answer) && answer != null){
                dataRowOrder.addAttribute("OPTION_ANSWER", answer);
            }else {
                if("12".equals(appointId)){
                    dataRowOrder.addAttribute("OPTION_ANSWER", "9");
                }else if("16".equals(appointId)){
                    dataRowOrder.addAttribute("OPTION_ANSWER", "10");
                }else if("17".equals(appointId)){
                    dataRowOrder.addAttribute("OPTION_ANSWER", "");
                }else if("18".equals(appointId)){
                    dataRowOrder.addAttribute("OPTION_ANSWER", "999");
                }else if("19".equals(appointId) || "20".equals(appointId)){
                    dataRowOrder.addAttribute("OPTION_ANSWER", "");
                }else {
                    dataRowOrder.addAttribute("OPTION_ANSWER", "1");
                }
//                if(!appointId.contains("12,16,17,18,19,20")){
//                    dataRowOrder.addAttribute("OPTION_ANSWER", "1");
//                }else if("12".contains(appointId)){
//                    dataRowOrder.addAttribute("OPTION_ANSWER", "9");
//                }else if("16".contains(appointId)){
//                    dataRowOrder.addAttribute("OPTION_ANSWER", "10");
//                }else if("17".contains(appointId)){
//                    dataRowOrder.addAttribute("OPTION_ANSWER", "15");
//                }else{
//                    dataRowOrder.addAttribute("OPTION_ANSWER", "999");
//                }
            }

            if(!"".equals(cause) && cause != null){
                dataRowOrder.addAttribute("OPINION", cause);
            }else {
                dataRowOrder.addAttribute("OPINION", "");
            }
            dataRowOrder.addAttribute("CHANNEL", "微信").addAttribute("PATIENT_ID", "").addAttribute("PATIENT_NAME", name);
            dataRowOrder.addAttribute("VISIT_NO", "").addAttribute("PHONE_NO", phone).addAttribute("DEPT_CODE", "");
            if(!"".equals(deptName) && deptName != null){
                dataRowOrder.addAttribute("DEPT_NAME", deptName);
            }else {
                dataRowOrder.addAttribute("DEPT_NAME", "");
            }
        }
        String data = tplDoc.asXML();
        logger.info("data:"+data);
        Client client = WsClientFactory.getInstance().getWsClient(hisWsUrl);
        Object[] results = client.invoke("send",data);
        String originalResXmlMessage = (String) results[0];
        logger.info("originalResXmlMessage:::::::::::::"+originalResXmlMessage);
        Document document = DocumentHelper.parseText(originalResXmlMessage);
        Element resultInfo = document.getRootElement().element("Result").element("Info");
        String executeStatus = resultInfo.attributeValue("EXECUTE_STATUS");
        String executeMessage = resultInfo.attributeValue("EXECUTE_MESSAGE");
        logger.info("executeMessage::::::::::::::::::"+executeMessage);
        return executeStatus;
    }


    public Document getTplDoc(String method) {
        Document document = DocumentHelper.createDocument();
        Element root = document.addElement("BITCRoot");
        root.addElement("CommitData");
        root.addElement("ReturnData");
        Element clientInfo = root.addElement("ClientInfo");
        Element clientInfoElement = clientInfo.addElement("Info");
//        clientInfoElement.addAttribute("METHOD", method).addAttribute("OPT_ID", "1611").addAttribute("OPT_NAME", "微信公众").addAttribute("OPT_IP", "192.168.1.106").addAttribute("OPT_DATE", DataFormatUtils.getTimeString(new Date())).addAttribute("OPT_GUID", IdGenerator.getUUID().toLowerCase()).addAttribute("TOKEN", "").addAttribute("OPT_ACCT", "");
        clientInfoElement.addAttribute("METHOD", method).addAttribute("OPT_ID", "2140").addAttribute("OPT_NAME", "微信公众").addAttribute("OPT_IP", "192.168.1.106").addAttribute("OPT_DATE", DataFormatUtils.getTimeString(new Date())).addAttribute("OPT_GUID", IdGenerator.getUUID().toLowerCase()).addAttribute("TOKEN", "").addAttribute("OPT_ACCT", "");
        Element serverInfo = root.addElement("ServerInfo");
        Element infoElement = serverInfo.addElement("Info");
        infoElement.addAttribute("DEV_ID", "").addAttribute("DEV_IP", "").addAttribute("DEV_DATE", "").addAttribute("DEV_GUID", "").addAttribute("DEV_ACCT", "");
        Element resultInfoElement = root.addElement("Result").addElement("Info");
        resultInfoElement.addAttribute("EXECUTE_STATUS", "").addAttribute("EXECUTE_MESSAGE", "");
        return document;
    }
}
